// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "grpc.h"


#ifndef FALSE
#define FALSE (1 == 0)
#endif

#ifndef TRUE
#define TRUE (0 == 0)
#endif

#define N    3


double A [N * N]  = {0, 1, 2, 3, 4, 5, 6, 7, 8};
double B [N * N]  = {1, 0, 0, 0, 1, 0, 0, 0, 1};
double C [N * N]  = {0, 0, 0, 0, 0, 0, 0, 0, 0};

extern int ninf_error_code;
int verbose = FALSE;

int matmul_test(){
		grpc_function_handle_t handle;
		grpc_function_handle_default(&handle, "matmul/matmul");

		grpc_error_t error;
		if ((error = grpc_call(&handle, N, A, B, C)) != GRPC_NO_ERROR)
				printf("matmul_test: %s\n", grpc_error_string(error));
		for (int i = 0; i < N * N; i++)
				printf("%f ", *(C+i));

		return TRUE;
}

int usage(){
  fprintf(stderr, "MatMulGrpc -config CONF [-verbose]\n");
  exit(1);
}


int main(int argc, const char ** argv)
{
  int configured = FALSE;
    while (argc > 1){
				argv++;
				if (strcasecmp(*(argv), "-verbose") == 0)
						verbose = TRUE;
				if (strcasecmp(*(argv), "-config") == 0) {
						grpc_initialize(*(++argv));
						argc--;
						configured = TRUE;
				}
				//      if (strcasecmp(*(argv), "-debug") == 0)
				//	ninf_debug_flag = TRUE;
				argc--;
    }
    if (! configured)
      usage();
    if (matmul_test())  {printf("\tOK\n");} else { printf("\tfailed\n");}

    grpc_finalize();

}

